import { observable, action } from 'mobx';
import * as Apis from "../../api/customerApis";
import * as adapter from '../../constants/adapter';
import { Toast, Bubble, Tools } from 'weaver-mobile';
import { BrowserHrm, ListViewPage, RouteLayout, MapPage, Form } from 'weaver-mobile-page';
const getLabel = Tools.getLabel;
export default class CustomerViewAndEditStore {

    comObj = {};
    @observable viewForm = {};
    @observable editForm = {};
    @observable customerId = "";
    @observable canEdit = false;//能否编辑
    @observable isFormInit = false;//是否获取到表单
    @observable isViewForm = false;//获取到查看
    @observable isEditForm = false;//获取到编辑
    @observable initSuccessed = false;
    @observable initFailedMsg = "";
    @observable buttonProps = { loading: false, disabled: false };
    DoNew = (param, comObj) => {
        this.comObj = comObj;
        this.sellChanceId = param.sellChanceId;
        this.customerId = param.customerId;
    }

    @action
    doInit = (params) => {
        Apis.getChanceForm({ sellChanceId: this.sellChanceId, operation: "view" ,...params}).then(data => {
            this.doInitCallBack(data);
        })
    }

    @action
    doInitCallBack = (data) => {
        const currentPath = this.comObj.props.location.pathname;
        this.isFormInit = true;
        if (data.status === "success") {
            this.viewForm = new Form.Store();
            this.viewForm.initForm(data.datas.formItemGroups, currentPath);
            this.isViewForm = true;
            this.isEditForm = false;
            this.initSuccessed = true;
        } else {
            this.initFailedMsg = data.msgcode;
            this.initSuccessed = false;
        }
    }

    //获取编辑表单
    @action
    getEditForm = () => {
        const currentPath = this.comObj.props.location.pathname;
        this.isFormInit = false;
        Apis.getChanceForm({ sellChanceId: this.sellChanceId, isView: false }).then(data => {
            if (data.status === "success") {
                this.editForm = new Form.Store();
                this.editForm.initForm(data.datas.formItemGroups, currentPath);
                this.isViewForm = false;
                this.isEditForm = true;
                this.initSuccessed = true;
            } else {
                this.initFailedMsg = data.msgcode;
                this.initSuccessed = false;
            }
            this.isFormInit = true;
        })
    }

    //取消编辑
    @action
    cancelEdit = () => {
        this.isFormInit = false;
        this.doInit();
    }

    //保存
    @action
    doSave = () => {
        const params = { ...this.editForm.formParams, customerId: this.customerId, sellChanceId: this.sellChanceId };
        if (this.editForm.doValidate().pass) {
            Toast.loading(getLabel('388626', '正在保存，请稍后...'), 0);
            Apis.sellChanceAllEdit(params).then(data => {
                if (data.status === "success") {
                    Toast.hide();
                    Bubble.success(data.msgcode, 1, { backgroundColor: adapter.BUBBLE_BGCOLOR }, 2, () => {
                        this.isFormInit = false;
                        this.doInit();
                    });
                } else {
                    Toast.hide();
                    Bubble.error(data.msgcode, 1, { backgroundColor: adapter.BUBBLE_BGCOLOR }, 2, () => {
                        this.isFormInit = false;
                        this.doInit();
                    });
                }
            });
        }
    }
    //删除商机
    @action
    doDelete = () => {
        const params = { sellChanceIds: this.sellChanceId };
        Apis.chanceDelete(params).then(data => {
            if (data.status === "success") {
                window.crmGlobal && window.crmGlobal.doSearch();
                Bubble.success(getLabel('83472', '删除成功!'), 1, { backgroundColor: adapter.BUBBLE_BGCOLOR }, 2,
                    // this.doInit()
                    () => {
                        this.comObj.props.history.go(-2);
                        if (typeof(this.comObj.props.cardStore!=="undefined")&&this.comObj.props.cardStore) {
                            if (this.comObj.props.cardStore.tabChange("chance")) {
                                this.comObj.props.cardStore.tabChange("chance");
                            } else {
                                this.comObj.props.cardStore.doInit();
                            }

                        }
                    }
                );
            }
        });
    }
    @action //修改商机来源的浏览按钮条件
    doChangeFormCondition = (domkey, param) => {
        if (this.editForm.isInit) {
            this.editForm.conditioninfo.forEach((group) => {
                group.items.forEach((field) => {
                    if (field.domkey.join('_') === domkey) {
                        if (param) {
                            field.browserConditionParam.dataParams.CustomerID = param;
                        }
                    }
                })
            })
        }
    }
    @action
    groupToggle = (...arg) => {
        const title = arg[0];
        const status = arg[1];
        this.editForm.conditioninfo = this.editForm.conditioninfo.map(c => ({
            ...c,
            ...(c.title === title ? { show: status } : {}),
        }));
    }
}